﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 企业号组织
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-08-06 
    /// </summary>
    internal class QYDepartmentService : DataFactory, QYDepartmentIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<QYDepartmentEntity>> GetList()
        {
            using (var db = this.Basic)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append(@"select
                                    o.F_Id,
                                    o.F_ParentId,
                                    o.F_EnCode,
                                    o.F_FullName,
                                    o.F_Description,
                                    w.F_SyncState,
                                    w.F_SubmitState,
                                    w.F_LastModifyTime
                                    from  Base_Organize o left join 
                                    WeChat_QYDepartment w on w.F_OrganizeId= o.F_Id");
                return await db.GetListSqlAsync<QYDepartmentEntity>(strSql.ToString());
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<QYDepartmentEntity>> GetSyncList()
        {
            using (var db = this.Basic)
            {
                return await db.GetListAsync<QYDepartmentEntity>();
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        public async Task<List<QYDepartmentEntity>> GetListByUserId(string userId)
        {
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                StringBuilder dbSql = new StringBuilder();
                dbSql.Append(@"select * from dbo.WeChat_QYDepartment where F_OrganizeId in(select F_OrganizeId from dbo.Base_User where  F_Id= @userId) order by F_SortCode");
                parameters.Add(DataHelper.CreateParameter("@userId", userId));
                return await db.GetListSqlAsync<QYDepartmentEntity>(dbSql.ToString(), parameters.ToArray());
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(QYDepartmentEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(QYDepartmentEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(QYDepartmentEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }
    }
}
